java文档打包成压缩包并且下载 - 小安妮的熊 - 博客园

创建时间:2019/11/20 10:24
来源:https://www.cnblogs.com/1246447850qqcom/p/9025692.html


java文档打包成压缩包并且下载

需求,根据产品ID查询产品详情,产品详情会返回产品的一些文案,以及图片的url。需要做成,将文案信息记录在一个txt文档中,然后图片下载到文件夹,最后下载到本地,下载后自动删除刚才生成的文件夹以及文件夹生成的压缩包等。

例如压缩包打开是这样的:

txt文档是这样的:

需求已明了,现在开始从页面点击下载开始action层:

由于我是每次用户点击页面下载时,生成文件到一个压缩包,压缩包存在项目的路径下的,下载成功后删除文件以及压缩包。

复制代码
/**
     * 获取当前产品的文案
     * 
     * @return
     * @throws IOException
     */
    public Resolution getZip() throws IOException {
        HttpServletResponse response = getContext().getResponse();
        Integer i = Integer.valueOf(gysupplierproducts.getProd_code());//
        JSONArray recal = getDealInfo.getDealInfoForOne(i);//得到产品信息json串
        String url = "";
        String name = "";
        String fileDir = "";
        for (int n = 0; n < recal.size(); n++) {
            Map<String, Object> returnMap = gysupplierproductsService.saveZip(recal.get(n).toString());//解析json串
            url = returnMap.get("url").toString();
            name = returnMap.get("name").toString();
            fileDir = url;
            try {
                 url = url+".zip";
                try {                    
                    File file = new File(url);
                    File file2 = new File(fileDir);                    
                    name = name+".zip";
                    response.setCharacterEncoding("UTF-8");
                    response.setHeader("Content-Disposition",
                            "attachment; filename=" + new String(name.getBytes("ISO8859-1"), "UTF-8"));
                    response.setContentLength((int) file.length());
                    response.setContentType("application/zip");// 定义输出类型
                    FileInputStream fis = new FileInputStream(file);
                    BufferedInputStream buff = new BufferedInputStream(fis);
                    byte[] b = new byte[1024];// 相当于我们的缓存
                    long k = 0;// 该值用于计算当前实际下载了多少字节
                    OutputStream myout = response.getOutputStream();// 从response对象中得到输出流,准备下载
                    // 开始循环下载
                    while (k < file.length()) {
                        int j = buff.read(b, 0, 1024);
                        k += j;
                        myout.write(b, 0, j);
                    }
                    myout.flush();
                    buff.close();
                    file.delete();//删除生成的压缩包文件
                    ZipUtils.delAllFile(fileDir);//文件夹下面还有东西,需要全部删除。
                    file2.delete(); //删除生产的文件
                } catch (Exception e) {
                    System.out.println(e);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
复制代码

service层,解析JSONArry,记录文本信息到txt,下载图片到指定文件夹下。

省略一解析部分:

复制代码
public Map<String,Object> saveZip(String delStr) throws IOException{
        Map<String,Object> returnMap = new HashMap<String, Object>();
        returnMap.put("url", "");
        returnMap.put("name", "");
        JSONObject dealInfo = JSONObject.fromObject(delStr);
        File directory = new File("");// 参数为空
        String courseFile = directory.getCanonicalPath();
        //I:\workspace\qhzn-fxxt
        String fileName = Comb.getComb().replaceAll("-","");
        courseFile = courseFile+"\\webapp\\zip\\"+fileName;
        //I:\workspace\qhzn-fxxt/webapp/zip/425a0c0490704706813593d080329037
        System.out.println(courseFile);
        File dirFile = new File(courseFile);
        dirFile.mkdir();//创建文件夹
        DealBaseInfo baseInfo = (DealBaseInfo)JSONObject.toBean(dealInfo.getJSONObject("baseInfo"), DealBaseInfo.class) ;
        String title = baseInfo.getTitle();//产品名称
        JSONArray inageArray = JSONArray.fromObject(baseInfoObj.get("imgInfos"));
        List<ImageInfo> inageList = JSONArray.toList(inageArray, new ImageInfo(), new JsonConfig());
//        List<ImageInfo> inageList = baseInfo.getImgInfos();
        for (int i = 0; i < inageList.size(); i++) {
            Integer frontImage =inageList.get(i).getFrontImage();
            String imgName = "";
            if(1==frontImage){
                //首图
                imgName = "首图"+i;
                this.downloadPicture(inageList.get(i).getImageUrl(), courseFile,imgName);
            }else{
                //
                imgName = "内容图"+i;
                this.downloadPicture(inageList.get(i).getImageUrl(), courseFile,imgName);
            }            
        }
                
        StringBuffer buffer = new StringBuffer();
        
        buffer.append("产品名:"+title+"\r\n");        
        buffer.append("可用星期可用天数 :"+this.strToWeek(validWeekRule)+"\r\n");        
        //将解析的文字记录到txt
        FileMT.saveFile(fileName, buffer.toString(),courseFile);
        
        //压缩文件 courseFile
        FileOutputStream fos1 = new FileOutputStream(new File(courseFile+".zip"));
        ZipUtils.toZip(courseFile, fos1, false);
        returnMap.put("url", courseFile);//返回生成的文件夹名称和路径
        returnMap.put("name", fileName);
        return returnMap;
    }
复制代码

用到的公共方法:在解析周的时候,做了特殊处理,因为产品信息返回的周例如:"1101000"表示周一周二和周四可用,所以我们给用户展现需要转换过来。

一下是用到了方法:1101000转换星期

复制代码
public static String strToWeek(String str){
        String week="";
        String[] weekArr={"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
        for(int i=0;i<str.length();i++){
            String curStr=str.substring(i, i+1);
            if("1".equals(curStr)){
                week+=weekArr[i]+",";
            }
        }
        if(StringUtils.isNotBlank(week)){
            week=week.substring(0,week.length()-1);
        }
        return week;
    }
复制代码

下载网络图片:

复制代码
/***
*urlList == 网络路径;path===下载存放的位置;imgName==图片名称
**/
private static void downloadPicture(String urlList,String path,String imgName) {  
        URL url = null;  
        try {  
            url = new URL(urlList);  
            DataInputStream dataInputStream = new DataInputStream(url.openStream());  
  
            FileOutputStream fileOutputStream = new FileOutputStream(new File(path+"\\"+imgName+".jpg"));  
            ByteArrayOutputStream output = new ByteArrayOutputStream();  
  
            byte[] buffer = new byte[1024];  
            int length;  
  
            while ((length = dataInputStream.read(buffer)) > 0) {  
                output.write(buffer, 0, length);  
            }  
            fileOutputStream.write(output.toByteArray());  
            dataInputStream.close();  
            fileOutputStream.close();  
        } catch (MalformedURLException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
}
复制代码

FileMT类,保存成txt文本。

复制代码
public static String saveFile(String fileName,String content,String url){
        String result="";
        try {
            contentToTxt(url+"/"+fileName+".txt", content);
            result="1";
        } catch (Exception e) {
            result="2";
            e.printStackTrace();
        }
         return result;
}

public static void contentToTxt(String filePath, String content) {  
            String str = new String();
            String s1 = new String();
            try {  
                File f = new File(filePath); 
                File parent = f.getParentFile(); 
                if(parent!=null&&!parent.exists()){
                    parent.mkdirs();
                }
                f.createNewFile();
                if (f.exists()) {  
                    System.out.print("文件存在");  
                } else {  
                    System.out.print("文件不存在");  
                    f.createNewFile();
                }  
                
                BufferedReader input = new BufferedReader(new FileReader(f));  
                while ((str = input.readLine()) != null) {  
                    s1 +="";
                }  
                input.close(); 
                content= new String(content.getBytes("UTF-8"),"UTF-8");
                s1 += content;  
                BufferedWriter output = new BufferedWriter(new FileWriterWithEncoding(f, "utf-8"));  
                output.write(s1);  
                output.close();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
}
复制代码

ZipUtils类,主要是压缩文件和删除文件夹工具类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
import com.tmwsoft.util.EncryptPassword;
 
public class ZipUtils {
 
    private static final int BUFFER_SIZE = 2 * 1024;
 
    /**
     * 压缩成ZIP 方法1
     *
     * @param srcDir
     *            压缩文件夹路径
     * @param out
     *            压缩文件输出流
     * @param KeepDirStructure
     *            是否保留原来的目录结构,true:保留目录结构;
     *            false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
     * @throws RuntimeException
     *             压缩失败会抛出运行时异常
     */
    public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure) throws RuntimeException {
        long start = System.currentTimeMillis();
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(out);
            File sourceFile = new File(srcDir);
            compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure);
            long end = System.currentTimeMillis();
            System.out.println("压缩完成,耗时:" + (end - start) + " ms");
        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipUtils", e);
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
    }
 
    /**
     * 递归压缩方法
     *
     * @param sourceFile
     *            源文件
     * @param zos
     *            zip输出流
     * @param name
     *            压缩后的名称
     * @param KeepDirStructure
     *            是否保留原来的目录结构,true:保留目录结构;
     *            false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
     * @throws Exception
     */
    private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure)
            throws Exception {
        byte[] buf = new byte[BUFFER_SIZE];
        if (sourceFile.isFile()) {
            // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
            zos.putNextEntry(new ZipEntry(name));
            // copy文件到zip输出流中
            int len;
            FileInputStream in = new FileInputStream(sourceFile);
            while ((len = in.read(buf)) != -1) {
                zos.write(buf, 0, len);
            }
            // Complete the entry
            zos.closeEntry();
            in.close();
        } else {
            File[] listFiles = sourceFile.listFiles();
            if (listFiles == null || listFiles.length == 0) {
                // 需要保留原来的文件结构时,需要对空文件夹进行处理
                if (KeepDirStructure) {
                    // 空文件夹的处理
                    zos.putNextEntry(new ZipEntry(name + "/"));
                    // 没有文件,不需要文件的copy
                    zos.closeEntry();
                }
 
            } else {
                for (File file : listFiles) {
                    // 判断是否需要保留原来的文件结构
                    if (KeepDirStructure) {
                        // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
                        // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
                        compress(file, zos, name + "/" + file.getName(), KeepDirStructure);
                    } else {
                        compress(file, zos, file.getName(), KeepDirStructure);
                    }
 
                }
            }
        }
    }
 
    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
                delFolder(path + "/" + tempList[i]);// 再删除空文件夹
                flag = true;
            }
        }
        return flag;
    }
 
    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); // 删除完里面所有内容
            String filePath = folderPath;
            filePath = filePath.toString();
            java.io.File myFilePath = new java.io.File(filePath);
            myFilePath.delete(); // 删除空文件夹
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) throws FileNotFoundException {
        /** 测试压缩方法1 */
        FileOutputStream fos1 = new FileOutputStream(new File("f:/新建文本文档.zip"));
        ZipUtils.toZip("f:/新建文本文档.txt", fos1, true);
         
 
    }
 
}

  

 

 

 

 

  

 

 

 

 

 

 

 

 

 

 

 

 最后来个声明,以上所有资料都是我在网上找的别人的文章,然后复制代码变成我的了,已经找不到具体哪一篇。

时间,请带我像一条小溪流般,安静地流淌,汇入爱的海洋。
posted @ 2018-05-11 17:15 小安妮的熊 Views(1305) Comments(0) Edit 收藏